package com.itcat.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.api.mysql.PmsProductCategoryApi;
import com.itcat.mapper.PmsProductAttributeMapper;
import com.itcat.mapper.PmsProductCategoryMapper;
import com.itcat.mysql.param.PageParam;
import com.itcat.mysql.pojo.PmsProductAttribute;
import com.itcat.mysql.pojo.PmsProductCategory;
import com.itcat.mysql.pojo.PmsProductCategoryDto;
import com.itcat.mysql.vo.PmsProductCategoryVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author Jerry Broker
 * @project_name rainbow-mall
 * @create 2022-04-02 18:38
 */
@DubboService
public class PmsProductCategoryApiImpl implements PmsProductCategoryApi {

    @Autowired
    private PmsProductCategoryMapper pmsProductCategoryMapper;

    @Autowired
    private PmsProductAttributeMapper pmsProductAttributeMapper;

    /**
     * 获取parentId为i的产品分类
     * @param i
     * @return
     */
    @Override
    public List<PmsProductCategory> getProductCategory(Long i) {
        LambdaQueryWrapper<PmsProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsProductCategory::getParentId, i);
        List<PmsProductCategory> list = pmsProductCategoryMapper.selectList(queryWrapper);
        return list;
    }

    /**
     * 通过parentId查询分类信息
     * @param parentId
     * @param pageParam
     * @return
     */
    @Override
    public Page<PmsProductCategory> listByParentId(Integer parentId, PageParam pageParam) {
        Page<PmsProductCategory> page = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
        LambdaQueryWrapper<PmsProductCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PmsProductCategory::getParentId,parentId);
        pmsProductCategoryMapper.selectPage(page, lambdaQueryWrapper);
        return page;
    }

    /**
     * 根据PmsProductCategoryApi的Id查询PmsProductCategoryApi
     * @param productCategoryId
     * @return
     */
    @Override
    public PmsProductCategory getProductCategoryById(Long productCategoryId) {
        PmsProductCategory pmsProductCategory = pmsProductCategoryMapper.selectById(productCategoryId);
        return pmsProductCategory;
    }

    @Override
    public void create(PmsProductCategoryDto dto) {

        pmsProductCategoryMapper.insert(dto);
        List<Integer> productAttributeIdList = dto.getProductAttributeIdList();
        for (Integer aLong : productAttributeIdList) {
            PmsProductAttribute pmsProductAttribute = pmsProductAttributeMapper.selectById(aLong);
        }
    }



    @Override
    public void updateShowStatus(Integer ids, Integer navStatus) {
        PmsProductCategory pmsProductCategory = pmsProductCategoryMapper.selectById(ids);
        pmsProductCategory.setShowStatus(navStatus);
        pmsProductCategoryMapper.updateById(pmsProductCategory);
    }

    @Override
    public void updateNavStatus(Integer ids, Integer navStatus) {
        PmsProductCategory pmsProductCategory = pmsProductCategoryMapper.selectById(ids);
        pmsProductCategory.setNavStatus(navStatus);
        pmsProductCategoryMapper.updateById(pmsProductCategory);
    }

    @Override
    public void deleteById(Integer parentId) {
        pmsProductCategoryMapper.deleteById(parentId);
    }

    @Override
    public PmsProductCategory selectById(Integer parentId) {
        PmsProductCategory pmsProductCategory = pmsProductCategoryMapper.selectById(parentId);
        return pmsProductCategory;
    }
}
